package com.ll.daipao.wechat.service;

import com.alibaba.fastjson.JSONObject;
import com.ll.daipao.wechat.dao.TAccountMapper;
import com.ll.daipao.wechat.dao.TOrderMapper;
import com.ll.daipao.wechat.dao.TOrderTypeMapper;
import com.ll.daipao.wechat.dao.TSchoolMapper;
import com.ll.daipao.wechat.vo.TAccount;
import com.ll.daipao.wechat.vo.TOrder;
import com.ll.daipao.wechat.vo.TOrderType;
import com.ll.daipao.wechat.vo.TSchool;
import io.swagger.models.auth.In;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by rockyll on 2017/10/13.
 */
@Service
public class OrderService {

    @Autowired
    TOrderMapper tOrderMapper;

    @Autowired
    TAccountMapper tAccountMapper;

    @Autowired
    TSchoolMapper tSchoolMapper;

    @Autowired
    TOrderTypeMapper tOrderTypeMapper;


    public List<Map<String,Object>> selectAll(Map<String ,Object> map){
            return tOrderMapper.selectAll(map);
    }

    /**
     * 通过openid 查询所有的发单
     * @param openId  openid
     * @return
     */
    public List<Map<String,Object>>   mineObtainOrderlist(String openId){
        return tOrderMapper.selectAllHeOrderByOpenId(openId);
    }

    //发单
    public List<Map<String,Object>> selectAllHeOrderByMap(Map<String,Object> map){
        return tOrderMapper.selectAllHeOrderByMap(map);
    }
    //接单
    public List<Map<String,Object>> selectAllReOrderByMap(Map<String,Object> map){
        return tOrderMapper.selectAllReOrderByMap(map);
    }


    public JSONObject myhome(String openId){
        JSONObject res = new JSONObject();
        TAccount account = tAccountMapper.selectAccountByOpenId(openId);
        if(account==null){
            return res;
        }
        String id = String.valueOf(account.getId());
        Integer schoolId = account.getSchoolId();
        if(schoolId!=null){
            TSchool tSchool = tSchoolMapper.selectByPrimaryKey(schoolId);
            res.put("tSchool",tSchool);
        }
        int  sendcount = tOrderMapper.selectAllOrderBysend(id);
        Map<String ,Object > map = tOrderMapper.selectAllOrderAmountByre(id);
        if(map!=null){
            res.put("sendcount",sendcount);
            res.put("sums",map.get("sums"));
            res.put("accepts",map.get("accepts"));
        }
        res.put("account",account);
        return res;
    }


    public List<TOrderType> selectOrderType(){
        return tOrderTypeMapper.selectOrderType();
    }

    public JSONObject selectDetail(Integer id ,String openId){
        JSONObject res = new JSONObject();
        TAccount tAccount = tAccountMapper.selectAccountByOpenId(openId);
        if(tAccount==null){
            return res;
        }
        Integer reid = tAccount.getId();
        Map<String,Object> map = new HashMap<>();
        map.put("id",id);
        map.put("reid",tAccount.getId());
        Map<String, Object> stringObjectMap = tOrderMapper.selectOrderByIdAndReaccountId(map);
        res.put("order",stringObjectMap);
        return res;
    }

    public List<Map<String, Object>> selectAllMoney(String openid){
        return tOrderMapper.selectAllHeOrderByOpenId(openid);
    }

    @Transactional(rollbackFor = Exception.class)
    public JSONObject saveOrder(String openId , TOrder tOrder) throws Exception{
        JSONObject res = new JSONObject();
        TAccount tAccount = tAccountMapper.selectAccountByOpenId(openId);
        if(tAccount==null){
            res.put("suc",false);
            res.put("msg","没找到相关登录人信息");
            return res;
        }
        String orderType = tOrder.getOrderType();
        TOrderType tOrderType = tOrderTypeMapper.selectByPrimaryKey(Integer.parseInt(orderType));

        if(tOrderType==null){
            res.put("suc",false);
            res.put("msg","未找到相关订单的类型");
            return res;
        }
        tOrder.setPayStatus("2");
        tOrder.setOrderStatus("1");
        tOrder.setHeAccountId(tAccount.getId());
        tOrderMapper.insertSelective(tOrder);
        res.put("suc",true);
        res.put("payamount",tOrderType.getPayAmount());
        return res;

    }

    @Transactional(rollbackFor = Exception.class)
    public JSONObject reOrder(String openId , String id)throws Exception{
        JSONObject res = new JSONObject();
        TAccount tAccount = tAccountMapper.selectAccountByOpenId(openId);
        if(tAccount==null){
            res.put("suc",false);
            res.put("msg","缺失重要数据！");
            return res;
        }
        TOrder tOrder = tOrderMapper.selectByPrimaryKey(Integer.parseInt(id));

        if(tOrder==null){
            res.put("suc",false);
            res.put("msg","找不到相关订单！");
            return res;
        }
        if(tOrder.getReAccountId()==tAccount.getId()){
            res.put("suc",true);
            return res;
        }
        if(!"1".equals(tOrder.getOrderStatus())){
            res.put("suc",false);
            res.put("msg","订单已被接单，请加快手速");
            return res;
        }
        tOrder.setOrderStatus("2");

        tOrder.setReAccountId(tAccount.getId());
        tOrder.setReOrderTime(new Date());
        tOrderMapper.updateByPrimaryKeySelective(tOrder);
        res.put("suc",true);
        return res;
    }


}
